package com.bzy.blp.upms.jcaptcha;

import com.bzy.common.util.LoggerUtil;
import com.octo.captcha.service.CaptchaServiceException;
import com.octo.captcha.service.captchastore.FastHashMapCaptchaStore;
import javax.servlet.http.HttpServletRequest;

public class JCaptcha {
	public static final MyManageableImageCaptchaService captchaService = new MyManageableImageCaptchaService(
			new FastHashMapCaptchaStore(), new GMailEngine(), 180, 100000, 75000);

	public static boolean validateResponse(HttpServletRequest request, String userCaptchaResponse) {
		LoggerUtil.shiroLog("JCaptcha.java--validateResponse--userCaptchaResponse=" + userCaptchaResponse);
		
		if (request.getSession(false) == null) {
			LoggerUtil.shiroLog("JCaptcha.java--validateResponse--session is null");
			return false;
		}

		boolean validated = false;
		try {
			String id = request.getSession().getId();
			LoggerUtil.shiroLog("JCaptcha.java--validateResponse--session id=" + id);
			validated = captchaService.validateResponseForID(id, userCaptchaResponse).booleanValue();
			LoggerUtil.shiroLog("JCaptcha.java--validateResponse--validated=" + validated);
		} catch (CaptchaServiceException e) {
			e.printStackTrace();
		}
		return validated;
	}

	public static boolean hasCaptcha(HttpServletRequest request, String userCaptchaResponse) {
		if (request.getSession(false) == null)
			return false;
		boolean validated = false;
		try {
			String id = request.getSession().getId();
			validated = captchaService.hasCapcha(id, userCaptchaResponse);
		} catch (CaptchaServiceException e) {
			e.printStackTrace();
		}
		return validated;
	}

}
